<template>
    <div :class="className" :style="{height:height,width:width}">
    </div>
</template>

<script>
import echarts from 'echarts';
require('echarts/theme/macarons'); // echarts 主题
const animationDuration = 3000;
export default {
    name: 'charts-pie',
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        chartData: {
            type: Array,
            default: []
        },
        legendData: {
            type: Array,
            default: []
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '100%'
        },
        title: {
            type: String,
            default: null
        }
    },
    watch: {
        chartData: function(val) {
            this.initChart()
        },
    },
    data() {
        return {
            chart: null,
        };
    },
    mounted() {
        // this.initChart();
        this.chart = echarts.init(this.$el, 'macarons');
        this.initChart()
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose();
        this.chart = null;
    },
    methods: {
        initChart() {
            if (!this.chart) {
                return
            }
            this.chart.setOption({
                title: {
                    text: this.title,
                    // subtext: 'CPU：6/12核',
                    left: 'center',
                    textStyle: {
                        fontSize: 16
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{b} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    top:250,
                    // bottom: 50,
                    left: 'center',
                    data: this.legendData,
                    itemWidth: 15,
                    // formatter: function(name) {
                    //     return name;
                    // }
                },
                series: [
                    {
                        type: 'pie',
                        radius: 100,
                        center: ['50%', 135],
                        data: this.chartData,
                        label: {
                            normal: {
                                show: false
                            }
                        },
                    }
                ]
            })
        }
    }
}
</script>
